package com.focusai.efairy.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.focusai.efairy.R;
import com.focusai.efairy.model.location.MapType;
import com.focusai.efairy.ui.adapter.LocationMapTypeAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.utils.MapLocationUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luo_guangwen on 2017/10/10.
 */

public class PopWindowMapTypeView extends PopupWindow {
    private Context mContext;
    private RecyclerView mRecyclerView;
    private List<MapType> mList;
    private LocationMapTypeAdapter mapTypeAdapter;

    public PopWindowMapTypeView(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    private void init() {
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.view_list_text, null);
        this.setContentView(contentView);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setTouchable(true);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(mContext.getResources().getColor(R.color.transparent_background));
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popup_window_animation);
        mRecyclerView = (RecyclerView) contentView.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mapTypeAdapter = new LocationMapTypeAdapter(mContext);
        mRecyclerView.setAdapter(mapTypeAdapter);
        mList = new ArrayList<>();
        mapTypeAdapter.refreshData(mList);
        mapTypeAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseRecyclerViewHolder holder) {
                int position = mRecyclerView.getChildLayoutPosition(holder.itemView);
                MapType item = mList.get(position);
                if (item.getMapType() != 0){
                    MapLocationUtils.jumpToMap(mContext,item);
                }
                dismiss();
            }
        });
    }


    public void show(View view, double lat, double log, String adress) {
        if (this.isShowing())
            return;
        MapType mapType;
        mList.clear();
        if (MapLocationUtils.hasGaoDeMap(mContext)) {
            mapType = new MapType();
            mapType.setMapType(MapType.GAODE_MAP);
            mapType.setMode(0);
            mapType.setEndLongitude(log);
            mapType.setEndLatitude(lat);
            mapType.setEndName(adress);
            mapType.setName("高德地图");
            mList.add(mapType);

        }
        if (MapLocationUtils.hasBaiDuMap(mContext)) {
            mapType = new MapType();
            mapType.setMapType(MapType.BAIDU_MAP);
            mapType.setMode(0);
            mapType.setEndLongitude(log);
            mapType.setEndLatitude(lat);
            mapType.setEndName(adress);
            mapType.setName("百度地图");
            mList.add(mapType);
        }
        if (mList.size() == 0){
            Toast.makeText(mContext,"本地没找到高德地图或百度地图应用",Toast.LENGTH_SHORT).show();
            return;
        }
        mapType = new MapType();
        mapType.setMapType(0);
        mapType.setName("取消");
        mList.add(mapType);

        showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void dismiss() {
        if (this.isShowing()) {
            super.dismiss();
        }
    }
}
